//
//  CoinAddressView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class CoinAddressView: BaseView, UITableViewDataSource, UITableViewDelegate {
    var viewModel:CoinAddressViewModel?
    lazy var tableView:UITableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.grouped)
    override func setUpUI() {
        super.setUpUI()
        viewModel = dataSource as? CoinAddressViewModel
        
        viewModel?.addObserver(self, forKeyPath: "refreshBool", options: [.new,.old], context: nil)
        
        
        self.addSubview(tableView)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clear
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalToSuperview()
        }
        
    }
    
    /// KVO监听属性
    ///
    /// - Parameters:
    ///   - keyPath: 被监听的属性名
    ///   - object:
    ///   - change:
    ///   - context:
    @objc override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "refreshBool" {
            tableView.reloadData()
        }
        
    }
    
    //MARK tableViewDelegate
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return (viewModel?.dataArr.count)!
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identy = "identyCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identy) as? BaseTableViewCell
        if cell == nil {
            cell = BaseTableViewCell(style: .default, reuseIdentifier: identy)
            cell?.setArrowImage()
            cell?.contentView.addSubview((cell?.kindLabel)!)
            cell?.kindLabel.snp.makeConstraints({ (make) in
                make.right.equalToSuperview().offset(-35)
                make.centerY.equalToSuperview()
            })
        }
        let model = viewModel?.dataArr[indexPath.row]
        cell?.kindLabel.text = String(format: "%d", (model?.addressNumber)!)
        cell?.nameLabel.text = model?.coinCode.count != 0 ? model?.coinCode : ""
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 0.1))
        view.backgroundColor = UIColor.clear
        return view
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 0.1))
        view.backgroundColor = UIColor.clear
        return view
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let model = viewModel?.dataArr[indexPath.row]
        let controller = ExtractCoinViewController()
        controller.viewModel?.selectBool = (viewModel?.selectBool)!
        controller.viewModel?.coinCode = (model?.coinCode)!
        self.viewModel?.controller?.navigationController?.pushViewController(controller, animated: true)
    }
    deinit {
        self.viewModel?.removeObserver(self, forKeyPath: "refreshBool", context: nil)
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
